﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TripleCComiC
{
    public partial class UISetting : UserControl
    {
        public event EventHandler RatioXChanged;
        public event EventHandler RatioYChanged;
        public event EventHandler RatioZoomChanged;
        public event EventHandler BarCheckedChanged;
        public event EventHandler ButtonFontClick;
        public event EventHandler ButtonOKClick;
        
        public enum SortOption : int
        {
            DoNotSort = 0,
            ByFirstNum,
            ByLastNum,
            SystemDefine
        }
        /// <summary>
        /// Return if user decide to open program in fullscreen or not.
        /// </summary>
        public bool FullScreenChecked
        {
            set
            {
                check_FullScreen.Checked = value;
            }
        }
        /// <summary>
        /// Return if user decide to preload first image or not.
        /// </summary>
        public bool FirstChecked
        {
            get
            {
                return check_First.Checked;
            }
        }
        /// <summary>
        /// Return if user decide to preload last image or not.
        /// </summary>
        public bool LastChecked
        {
            get
            {
                return check_Last.Checked;
            }
        }
        /// <summary>
        /// Return how many image in front of current image should be preload.
        /// </summary>
        public int CacheBackward
        {
            get
            {
                return Convert.ToInt32(num_CacheBackward.Value);
            }
        }
        /// <summary>
        /// Return how many image after current image should be preload.
        /// </summary>
        public int CacheForward
        {
            get
            {
                return Convert.ToInt32(num_CacheForward.Value);
            }
        }
        /// <summary>
        /// Return how many image will in the cache.
        /// </summary>
        public int CachePoolLength
        {
            get
            {
                return CacheBackward * 2 + CacheForward * 2 + 2 +
                    ((check_First.Checked) ? 1 : 0) +
                    ((check_Last.Checked) ? 1 : 0);
            }
        }
        /// <summary>
        /// Return minimum value of CacheBackward and CacheForward.
        /// </summary>
        public int CacheMinimum
        {
            set
            {
                num_CacheBackward.Minimum = value;
                num_CacheForward.Minimum = value;
            }
        }
        /// <summary>
        /// Gets a value indicating whether sort method be selected.
        /// </summary>
        public SortOption SelectedSortOption
        {
            get
            {
                if (radio_ByFirstNum.Checked)
                {
                    return SortOption.ByFirstNum;
                }
                else if (radio_ByLastNum.Checked)
                {
                    return SortOption.ByLastNum;
                }
                else if (radio_SystemDef.Checked)
                {
                    return SortOption.SystemDefine;
                }
                else
                {
                    return SortOption.DoNotSort;
                }
            }
            set
            {
                switch (value)
                {
                    case SortOption.ByFirstNum:
                        radio_ByFirstNum.Checked = true;
                        break;
                    case SortOption.ByLastNum:
                        radio_ByLastNum.Checked = true;
                        break;
                    case SortOption.SystemDefine:
                        radio_SystemDef.Checked = true;
                        break;
                    case SortOption.DoNotSort:
                        radio_NotSort.Checked = true;
                        break;
                }
            }
        }
        /// <summary>
        /// Frame per Second.
        /// </summary>
        public double FPS
        {
            get
            {
                return Convert.ToDouble(num_FPS.Value);
            }
        }
        /// <summary>
        /// How fast in rewind mode. Frame frequency in rewind mode is FPS * RewindRatio.
        /// </summary>
        public double RewindRatio
        {
            get
            {
                return Convert.ToDouble(num_RewindRatio.Value);
            }
        }
        /// <summary>
        /// Return if user decide to show time line bar or not.
        /// </summary>
        public bool BarChecked
        {
            get
            {
                return check_Bar.Checked;
            }
        }
        /// <summary>
        /// Height of time line bar.
        /// </summary>
        public int BarHeight
        {
            get
            {
                return Convert.ToInt32(num_BarHeight.Value);
            }
        }
        /// <summary>
        /// Return if user decide to homing each image when play consecutive images.
        /// </summary>
        public bool HomingChecked
        {
            get
            {
                return check_Homing.Checked;
            }
        }
        /// <summary>
        /// Move ratio in vertical axis.
        /// </summary>
        public double RatioX
        {
            get
            {
                return Convert.ToDouble(num_RatioX.Value);
            }
        }
        /// <summary>
        /// Move ratio in horizontal axis.
        /// </summary>
        public double RatioY
        {
            get
            {
                return Convert.ToDouble(num_RatioY.Value);
            }
        }
        /// <summary>
        /// The multiplicative factor used by zoom in / out.
        /// </summary>
        public double RatioZoom
        {
            get
            {
                return Convert.ToDouble(num_RatioZoom.Value);
            }
        }
        /// <summary>
        /// Information of font.
        /// </summary>
        public string FontInformation
        {
            set
            {
                lbl_Font.Text = value;
            }
        }
        /// <summary>
        /// Font of all controls in current UI.
        /// </summary>
        public Font GlobalFont
        {
            set
            {
                this.Font = value;
                foreach (Control ctrl in this.Controls)
                {
                    ctrl.Font = this.Font;
                }
                btn_Font.Size = new Size(1, 1);
                btn_OK.Size = new Size(1, 1);
            }
        }

        public UISetting()
        {
            InitializeComponent();
        }

        public void LoadProperties()
        {
            var userDef = Properties.Settings.Default;

            check_Caching.Checked = userDef.EnableCaching;
            check_First.Checked = userDef.CachingFirst;
            check_Last.Checked = userDef.CachingLast;
            num_CacheBackward.Value = userDef.CachingBackword;
            num_CacheForward.Value = userDef.CachingForword;
            num_RatioX.Value = userDef.MoveRatioX;
            num_RatioY.Value = userDef.MoveRatioY;
            num_RatioZoom.Value = userDef.PerZoom;
            num_FPS.Value = userDef.FPS;
            num_RewindRatio.Value = userDef.Rewind;
            check_Bar.Checked = userDef.ShowTimeLine;
            num_BarHeight.Value = userDef.BarHeight;
            check_Homing.Checked = userDef.Homing;
        }

        public void SaveProperties()
        {
            var userDef = Properties.Settings.Default;

            userDef.EnableCaching = check_Caching.Checked;
            userDef.CachingFirst = check_First.Checked;
            userDef.CachingLast = check_Last.Checked;
            userDef.CachingBackword = num_CacheBackward.Value;
            userDef.CachingForword = num_CacheForward.Value;
            userDef.MoveRatioX = num_RatioX.Value;
            userDef.MoveRatioY = num_RatioY.Value;
            userDef.PerZoom = num_RatioZoom.Value;
            userDef.FPS = num_FPS.Value;
            userDef.Rewind = num_RewindRatio.Value;
            userDef.ShowTimeLine = check_Bar.Checked;
            userDef.BarHeight = num_BarHeight.Value;
            userDef.Homing = check_Homing.Checked;
            userDef.Fullscreen = check_FullScreen.Checked;
            userDef.SortOption = (int)SelectedSortOption;
        }

        private static void raiseEvent(EventHandler handler, object sender, EventArgs e)
        {
            if (handler != null)
            {
                handler(sender, e);
            }
        }

        private void num_RatioX_ValueChanged(object sender, EventArgs e)
        {
            raiseEvent(RatioXChanged, sender, e);
        }

        private void num_RatioY_ValueChanged(object sender, EventArgs e)
        {
            raiseEvent(RatioYChanged, sender, e);
        }

        private void num_RatioZoom_ValueChanged(object sender, EventArgs e)
        {
            raiseEvent(RatioZoomChanged, sender, e);
        }

        private void check_Caching_CheckedChanged(object sender, EventArgs e)
        {
            if (check_Caching.Checked)
            {
                check_First.Enabled = true;
                check_Last.Enabled = true;
                num_CacheBackward.Enabled = true;
                num_CacheForward.Enabled = true;
                check_First.Checked = true;
                check_Last.Checked = true;
            }
            else
            {
                check_First.Enabled = false;
                check_Last.Enabled = false;
                num_CacheBackward.Enabled = false;
                num_CacheForward.Enabled = false;
                check_First.Checked = false;
                check_Last.Checked = false;
            }
        }

        private void check_Bar_CheckedChanged(object sender, EventArgs e)
        {
            raiseEvent(BarCheckedChanged, sender, e);
        }

        private void btn_Font_Click(object sender, EventArgs e)
        {
            raiseEvent(ButtonFontClick, sender, e);
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            raiseEvent(ButtonOKClick, sender, e);
        }
    }
}
